(function($){
	var rules = new Array();
	var engine=new $.Engine();
	var methods = {
    	required : function(explain){
        	return this.each(function(){
            	rules.push({
                	'id':this.name,
                	'type':'required',
                	'explain':explain?explain:"该项为必填项"
            	});
        	});
    	},
		
    	selectRequired : function(explain){
        	return this.each(function(){
            	rules.push({
                	'id':this.name,
                	'type':'selectRequired',
                	'explain':explain?explain:"请选择一个选项"
            	});
        	});
    	},
		
    	selectLength : function(value,explain){
        	return this.each(function(){
            	rules.push({
                	'id':this.name,
                	'type':'selectLength',
					'value':value,
                	'explain':explain?explain:"请选择一个选项"
            	});
        	});
    	},				
	
    	email : function(explain){
        	return this.each(function(){  
            	rules.push({
                	'id':this.name,
                	'type':'email',
                	'explain':explain?explain:"该项为电子邮箱地址"
            	});
        	}); 
    	},
		
		min : function(value,explain){
        	return this.each(function(){
            	rules.push({
                    'id':this.name,
                    'type':'min',
                    'value':value,
                    'explain':explain?explain:"该项不能小于"+value
            	});
        	});
    	},
   		
		max : function(value,explain){
        	return this.each(function(){ 
            	rules.push({
                	'id':this.name,
                	'type':'max',
                	'value':value,
                	'explain':explain?explain:"该项不能大于"+value
            	});
        	});
    	},
		
    	minlength : function(value,explain ) {
        	return this.each(function(){ 
            	rules.push({
                	'id':this.name,
                	'type':'minlength',
                	'value':value,
                	'explain':explain?explain:"该项不能少于"+value+"个字符"
            	});
        	});
    	},
		
   		maxlength : function(value,explain){
        	return this.each(function(){ 
            	rules.push({
                	'id':this.name,
                	'type':'maxlength',
                	'value':value,
                	'explain':explain?explain:"该项不能超过"+value+"个字符"
            	});
        	});
    	},

    	number : function(explain){
        	return this.each(function(){  
            	rules.push({
                	'id':this.name,
                	'type':'number',
                	'explain':explain?explain:"该项为整数"
            	});
        	});
    	},
		
   		decimal : function(explain){
        	return this.each(function(){  
            	rules.push({
                	'id':this.name,
                	'type':'decimal',
                	'explain':explain?explain:"该项为数字"
            	});
        	});
    	},

    	ip : function(explain){
        	return this.each(function(){  
            	rules.push({
                	'id':this.name,
                	'type':'ip',
                	'explain':explain?explain:"该项为IP地址，格式如xxx.xxx.xxx.xxx"
            	});
        	});
    	},
		
    	port : function(inputName,explain){
        	return this.each(function(){   
            	rules.push({
                	'id':this.name,
                	'type':'port',
                	'explain':explain?explain:"该项为端口号，是一个不超过65536的整数"
            	});
        	});
    	},
		
		tel : function(explain){
        		return this.each(function(){
            		rules.push({
                		'id':this.name,
                		'type':'tel',
                		'explain':explain?explain:"电话号码格式不正确"
            		});
        		}); 
    	},
    	chineseName : function(explain){
        	return this.each(function(){ 
            	rules.push({
               	 	'id':this.name,
                	'type':'chineseName',
                	'explain':explain?explain:"姓名只能含汉字、大写字母、符号中的空格和点，且至少两个字，最多32个字"
           		 });
        	}); 
    	},
		
    	idCard : function(explain){
        	return this.each(function(){ 
            	rules.push({
               	 	'id':this.name,
                	'type':'idCard',
                	'explain':explain?explain:"身份证格式不正确"
           		 });
        	}); 
    	},
		
    	url : function(explain){
        	return this.each(function(){ 
            	rules.push({
               	 	'id':this.name,
                	'type':'url',
                	'explain':explain?explain:"网站地址格式不正确"
           		 });
        	}); 
    	},

    	zipcode : function(explain){
        	return this.each(function(){ 
            	rules.push({
               	 	'id':this.name,
                	'type':'zipcode',
                	'explain':explain?explain:"邮政编码格式错误"
           		 });
        	}); 
    	},
		
    	mobile : function(explain){
        	return this.each(function(){ 
            	rules.push({
               	 	'id':this.name,
                	'type':'mobile',
                	'explain':explain?explain:"手机号格式不正确"
           		 });
        	}); 
    	},
		
    	decimal : function(explain){
        	return this.each(function(){ 
            	rules.push({
               	 	'id':this.name,
                	'type':'decimal',
                	'explain':explain?explain:"请输入浮点数"
           		 });
        	}); 
    	},											
		
		//正则
		regExp:function(value,explain){
        	return this.each(function(){ 
            	rules.push({
               	 	'id':this.name,
					'value':value,
                	'type':'regExp',
                	'explain':explain?explain:"格式不正确"
           		 });
        	}); 		
		},		

    	//自定义规则
    	custome : function(validateFunc,explain){
        	return this.each(function(){
            	rules.push({
                	'id':this.name,
                	'type':'custome',
                	'value':validateFunc,
                	'explain':explain?explain:""
            	});
        	});
    	},	
			
		lin_formValidate : function(options) {
			var config = {
     			 errorCallBack : function(rule){},
				 successCallBack : function(rule){},
	 			 successText:'输入正确'
  			};
			if (options) { 
                $.extend( config, options );
            }
			var type=$(this).attr('type');
			var elementName=$(this).attr('name');
			switch(type)
			{
				case 'checkbox':
				case 'radio':
					$("input[type='"+type+"'][name='"+elementName+"']").bind('blur',eventValidateHandle);
					$("input[type='"+type+"'][name='"+elementName+"']").bind('change',eventValidateHandle);
				break; 
				case 'text':
				case 'textarea':
					$(this).bind('blur',eventValidateHandle);
				break;
				case 'select-one':
				case 'select-multiple':
					$(this).bind('blur',eventValidateHandle); 
					$(this).bind('change',eventValidateHandle);
				break;	
			}
			 
			 function eventValidateHandle()
			 {
				var id=this.name;
				var tip=new Tip(id);
            	var inputValue = getElementValue(this);
                var error = validateElement(inputValue,id);
				if(error){
					tip.setMsg(error.explain);
					tip.error();
					config.errorCallBack(error);
				}else
					{
						tip.setMsg(config.successText);
						tip.success();
						config.successCallBack(error);	
					}				 
			 }
			
    	}			
  	};
	
	function Tip(id)
	{
		this.id=id;
		this.msg='';
		this.setMsg=function(msg){
			this.msg=this.msg || msg;
		};
		this.error=function(){
			$('#'+this.id+'Tip').removeClass('lin_success').addClass('lin_error').html(this.msg).show();
		};
		this.success=function(){
			$('#'+this.id+'Tip').removeClass('lin_error').addClass('lin_success').html(this.msg);
		};
	}
	
	//获得值
	function getElementValue(inputElement)
	{
        var tag = inputElement.tagName.toLowerCase();//元素标签
		var elementName=inputElement.name;//元素name
		var type = inputElement.type;//元素类型
        var inputValue;
        if(tag == "button")return;
        if(tag == "select"){
			if(type=='select-one')
			{
				inputValue = $(inputElement).val(); 
			}else
				{
					inputValue = $("select[name="+elementName+"] option:selected").length;
				}      
        }
        if(tag == "input"){
            if(type == "hidden" || type == "submit" || type == "reset"||type=="button"||type=="image")return;
            if(type == "text" || type == "password"){
                inputValue = $(inputElement).val();
            }
            if(type == "checkbox" || type == "radio"){
              inputValue=$("input[type='"+type+"'][name='"+elementName+"']:checked").length;       
            }
        }
		return inputValue;
	}
	
	//验证
	function validateElement(inputValue,id)
	{
		if(rules && rules.length>0){
          for(var i=0;i<rules.length;i++){
              if(rules[i].id == id){
                  if(engine[rules[i].type]){
                      if(!engine[rules[i].type].call(this,inputValue,rules[i].value)){
                          return rules[i];
                      }
                  }
              }
          }
        }
	}
	
	function submitValidateHandle(form)
	{
		var success=true; 
		if(rules && rules.length>0){
          	for(var i=0;i<rules.length;i++){
				var $that=$("#"+rules[i].id,form);
				var inputValue = getElementValue($that[0]);
              	if(engine[rules[i].type]){
                      	if(!engine[rules[i].type].call($that,inputValue,rules[i].value)){
							success=false;
							var tip=new Tip(rules[i].id);
							tip.setMsg(rules[i].explain);
							tip.error();
                      	}
                  	}
          	}
        }
		return success;
	}

	$.fn.lin_formValidate = function(method){
    	if ( methods[method] ) {
      		return methods[ method ].apply( this, Array.prototype.slice.call(arguments, 1 ));
    	} 
		else if(typeof method === 'object'||! method ) {
      				return methods.lin_formValidate.apply(this, arguments);
    	} else {
      		alert( 'Method ' +  method + ' 方法不存在');
    	}	   
	}
	
	$.fn.submitValidate=function(callBackfunction){
		var $this=$(this);
		$this.submit(function(){
			var errorTip=$this.find('.error');
			if(errorTip.length==0)
			{
				var success=submitValidateHandle($this);
				if(success)
				{
					if(typeof callBackfunction === "function")
					{
						return callBackfunction();
					}
					else
						{
							return success;
						}	
				}
				else
					{
						return false;
					}
			}else
				{
					return false;
				}

		});
	}
	
})(jQuery);